package com.jp.knowledge.activity;

import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.github.anzewei.parallaxbacklayout.ParallaxHelper;
import com.jiayen.util.ToasUtil;
import com.jp.knowledge.R;
import com.jp.knowledge.model.InfoData;
import com.jp.knowledge.util.L;
import com.jp.knowledge.util.ShareBuilder;
import com.jp.knowledge.view.JpWebView;
import com.jp.knowledge.webUtil.X5WebView;
import com.tencent.smtt.export.external.interfaces.WebResourceRequest;
import com.tencent.smtt.export.external.interfaces.WebResourceResponse;
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebView;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.shareboard.SnsPlatform;
import com.umeng.socialize.utils.ShareBoardlistener;

import org.xutils.view.annotation.ViewInject;

/**
 * Created by 528787067@qq.com on 2017/2/9.
 */

public class WebBrowserActivity extends LoadingWebActivity implements View.OnClickListener {

    @ViewInject(R.id.top_return)
    private ImageView returnBtn;
    @ViewInject(R.id.top_more)
    private ImageView moreBtn;

    private View popupView;
    private TextView shareBtn;
    private TextView refreshBtn;
    private PopupWindow morePopupWindow;

    public static void openWebActivity(Context context, String url){
        context.startActivity(new Intent(context, WebBrowserActivity.class)
                .putExtra("loadingUrl", url));
    }

    @Override
    protected int getResourceId() {
        return R.layout.activity_web_browser;
    }

    @Override
    protected void init() {
        super.init();
        jpWebView.setClearBeforeLoading(true);
        jpWebView.setWebViewCallback(new JpWebView.WebViewCallback(){
            @Override
            public void onPageStarted(WebView webView, String s, Bitmap bitmap) {}
            @Override
            public void onPageFinished(WebView webView, String s) {}
            @Override
            public void onReceivedError(WebView webView, int i, String s, String s1) {}
            @Override
            public void onReceivedHttpError(WebView webView, WebResourceRequest webResourceRequest, WebResourceResponse webResourceResponse) {}
            @Override
            public void onReceivedTitle(WebView view, String title) {}
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if(newProgress >= 30)
                    jpWebView.getLoadingAnimationView().setVisibility(View.GONE);
            }
        });

        jpWebView.getWebView().setOnTouchListener(new View.OnTouchListener() {
            private float doneY;
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(jpWebView.getLoadingAnimationView().getVisibility() != View.VISIBLE)
                    return false;
                if(event.getAction() == MotionEvent.ACTION_DOWN)
                    doneY = event.getY();
                else if(event.getAction() == MotionEvent.ACTION_MOVE
                        && Math.abs(event.getY() - doneY) > 50)
                    jpWebView.getLoadingAnimationView().setVisibility(View.GONE);
                return false;
            }
        });

        popupView = View.inflate(mContext, R.layout.dialog_web_browser, null);
        popupView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        shareBtn = (TextView) popupView.findViewById(R.id.share);
        refreshBtn = (TextView) popupView.findViewById(R.id.refresh);
        shareBtn.setOnClickListener(this);
        refreshBtn.setOnClickListener(this);
        morePopupWindow = new PopupWindow(popupView, popupView.getMeasuredWidth(), popupView.getMeasuredHeight(), true);
        morePopupWindow.setOutsideTouchable(true);
        morePopupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().setStatusBarColor(0x00000000);
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
            int statusBarHeight = 0;
            int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
            if (resourceId > 0)
                statusBarHeight = getResources().getDimensionPixelSize(resourceId);
            ((RelativeLayout.LayoutParams)returnBtn.getLayoutParams()).topMargin += statusBarHeight;
            ((RelativeLayout.LayoutParams)moreBtn.getLayoutParams()).topMargin += statusBarHeight;
        }

        returnBtn.setOnClickListener(this);
        moreBtn.setOnClickListener(this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        String url = intent.getStringExtra("loadingUrl");
        if(url != null)
            jpWebView.loadUrl(url);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            ParallaxHelper.disableParallaxBack(this);
        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
            ParallaxHelper.enableParallaxBack(this);
        }
        morePopupWindow.dismiss();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.top_return:
                onBackPressed();
                break;
            case R.id.top_more:
                popupView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
                morePopupWindow.showAsDropDown(moreBtn,
                        -morePopupWindow.getWidth()+moreBtn.getWidth(), 0);
                break;
            case R.id.share:
                morePopupWindow.dismiss();
                share();
                break;
            case R.id.refresh:
                morePopupWindow.dismiss();
                jpWebView.reload();
                break;
        }
    }

    private void share(){
        String url = jpWebView.getUrl();
        String text = "【" + jpWebView.getWebView().getTitle() + "】" + url;
        new ShareBuilder(this)
                .withTitle(mContext.getResources().getString(R.string.app_name))
                .withText(text)
                .withMedia(new UMImage(mContext, R.mipmap.icon_launcher))
                .withCopyContent(text)
                .withTargetUrl(url)
                .withQRCodeUrl(url)
                .build().open();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK && jpWebView.goBack())
            return true;
        return super.onKeyDown(keyCode,event);
    }
}
